/**
 * 
 */
package com.objectdraw.server.commands;

import com.objectdraw.server.CommunicationServer;
import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.data.Action;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;

/**
 * Processes a key command from the client.
 * 
 * @author jgulik, cfruehan, Harrison
 *
 */
public class KeyboardCommand extends Command {
	static final private String myCommand = "key";
	/* (non-Javadoc)
	 * @see com.objectdraw.server.commands.Command#processRequest(com.objectdraw.server.RequestData, com.objectdraw.server.ResponseData)
	 */
	/**
	 * Takes a message with the command "key" and sends the action to the current tool.  Also broadcasts
	 * the message to all listening clients.
	 */
	@Override
	public void processRequest(RequestData request, ResponseData response) {
		if(myCommand.equalsIgnoreCase(request.getCommand())){

			String[] params = request.getParams().split(" ");
			
			Action act = new Action();
			act.setArguments(params[0]);
			act.setAction(myCommand);
			act.setEvent("pressed");
			ObjectDrawServer.getInstance().addCanvasAction(act);
			
			response.addResponse("key " + params[0]);
			CommunicationServer.broadcast(response, ObjectDrawServer.getInstance().getViewingClientSockets());
			
			System.out.println(myCommand +" Command");
		}
		
		//Pass the request on to the next person in line
		if(this.successor != null){
			this.successor.processRequest(request,response);
		}

	}

}
